package com.mingqijia.gassafety.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.mingqijia.gassafety.db.entity.*;
import com.mingqijia.gassafety.db.mapper.RecordControllerMapper;
import com.mingqijia.gassafety.job.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 控制器上报数据 服务实现类
 * </p>
 *
 * @author guoliang.lao
 * @since 2022-08-03
 */
@Service
@Slf4j
public class RecordControllerServiceImpl extends ServiceImpl<RecordControllerMapper, RecordController> implements RecordControllerService {

    @Autowired
    RecordDetectorService recordDetectorService;
    @Autowired
    RecordReceiverValveService recordReceiverValveService;
    @Autowired
    RecordDtuService recordDtuService;
    @Autowired
    RecordControllerHistoryService controllerHistoryService;
    @Autowired
    RecordDetectorHistoryService detectorHistoryService;
    @Autowired
    RecordDtuHistoryService dtuHistoryService;
    @Autowired
    RecordReceiverValveHistoryService receiverValveHistoryService;

    @Override
    public List<RecordController> getListByTime(int page, int pageSize, String startTime, String endTime, int isHistory) {
        log.info("RecordControllerServiceImpl getListByTime,page:{},pageSize:{},startTime:{},endTime:{}",page,pageSize,startTime,endTime);
        PageHelper.startPage(page, pageSize);
        LambdaQueryWrapper<RecordController> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(RecordController::getCreatedAt,startTime,endTime)
                .eq(RecordController::getIsHistory,isHistory)
                .orderByAsc(RecordController::getCreatedAt);
        return this.list(queryWrapper);
    }

    @Override
    public List<RecordController> getPageList(int page, int pageSize) {
        log.info("RecordControllerServiceImpl getPageList,page:{},pageSize:{}",page,pageSize);
        PageHelper.startPage(page, pageSize);
        LambdaQueryWrapper<RecordController> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(RecordController::getCreatedAt);
        return this.list(queryWrapper);
    }

    @Override
    @Transactional
    public void deviceRecordHandle(RecordController recordController, List<RecordDetector> detectorList,
                                   List<RecordReceiverValve> receiverList, List<RecordDtu> dtuList, int page) {
        //写入历史表
        RecordControllerHistory controllerHistory = new RecordControllerHistory();
        BeanUtils.copyProperties(recordController, controllerHistory);
        controllerHistory.setOriginId(recordController.getId());
        log.info("RecordControllerHistory写入历史表：" + controllerHistory);
        String format = new SimpleDateFormat("MM").format(recordController.getCreatedAt());
        if (controllerHistoryService.saveData(controllerHistory, format)){
//            RecordController updateController = new RecordController();
//            updateController.setId(recordController.getId());
//            updateController.setIsHistory(true);
//            log.info("更新RecordController表为已备份到history表:"+updateController);
//            this.updateById(updateController);
            if (!CollectionUtils.isEmpty(detectorList)) {
                List<RecordDetectorHistory> recordDetectorHistoryList = new ArrayList();
                for (RecordDetector recordDetector : detectorList) {
                    RecordDetectorHistory recordDetectorHistory = new RecordDetectorHistory();
                    BeanUtils.copyProperties(recordDetector, recordDetectorHistory);
                    recordDetectorHistory.setOriginId(recordDetector.getId());
                    recordDetectorHistory.setOriginRecordId(recordController.getId());
                    recordDetectorHistory.setRecordId(controllerHistory.getId());
                    recordDetectorHistoryList.add(recordDetectorHistory);
                }
                log.info("RecordDetectorHistory写入历史表：" + recordDetectorHistoryList);
                detectorHistoryService.insertBatch(recordDetectorHistoryList, format);
            }
            if (!CollectionUtils.isEmpty(dtuList)) {
                List<RecordDtuHistory> recordDtuHistoryList = new ArrayList();
                for (RecordDtu recordDtu : dtuList) {
                    RecordDtuHistory recordDtuHistory = new RecordDtuHistory();
                    BeanUtils.copyProperties(recordDtu, recordDtuHistory);
                    recordDtuHistory.setOriginId(recordDtu.getId());
                    recordDtuHistory.setOriginRecordId(recordController.getId());
                    recordDtuHistory.setRecordId(controllerHistory.getId());
                    recordDtuHistoryList.add(recordDtuHistory);
                }
                log.info("RecordDtuHistory写入历史表：" + recordDtuHistoryList);
                dtuHistoryService.insertBatch(recordDtuHistoryList, format);
            }
            if (!CollectionUtils.isEmpty(receiverList)) {
                List<RecordReceiverValveHistory> receiverValveHistoryList = new ArrayList();
                for (RecordReceiverValve recordReceiverValve : receiverList) {
                    RecordReceiverValveHistory receiverValveHistory = new RecordReceiverValveHistory();
                    BeanUtils.copyProperties(recordReceiverValve, receiverValveHistory);
                    receiverValveHistory.setOriginRecordId(recordController.getId());
                    receiverValveHistory.setOriginId(recordReceiverValve.getId());
                    receiverValveHistory.setRecordId(controllerHistory.getId());
                    receiverValveHistoryList.add(receiverValveHistory);
                }
                log.info("RecordReceiverValveHistory写入历史表：" + receiverValveHistoryList);
                receiverValveHistoryService.insertBatch(receiverValveHistoryList, format);
            }
        }
        page++;
    }

    @Override
    @Transactional
    public void deleteRecord(List<Long> delIds, List<RecordDetector> detectorList, List<RecordReceiverValve> receiverList, List<RecordDtu> dtuList, int page) {
        log.info("删除的RecordController ids:"+delIds);
        if (!CollectionUtils.isEmpty(delIds)) {
            this.removeByIds(delIds);
        }
        if (!CollectionUtils.isEmpty(detectorList)) {
            recordDetectorService.removeByIds(detectorList.stream().map(a->a.getId()).collect(Collectors.toList()));
        }
        if (!CollectionUtils.isEmpty(receiverList)) {
            recordReceiverValveService.removeByIds(receiverList.stream().map(a->a.getId()).collect(Collectors.toList()));
        }
        if (!CollectionUtils.isEmpty(dtuList)) {
            recordDtuService.removeByIds(dtuList.stream().map(a->a.getId()).collect(Collectors.toList()));
        }
    }

    @Override
    @Transactional
    public void deleteRecordHistory(String month) {
        controllerHistoryService.deleteRecordHistory(month);
        detectorHistoryService.deleteRecordHistory(month);
        dtuHistoryService.deleteRecordHistory(month);
        receiverValveHistoryService.deleteRecordHistory(month);
    }
}
